capt log close _all
log using   "${logs}FigureA6" , replace text
*------------

// Time to run: around 3-4 minutes

*==================================================================
* Figure A.6: Residualized road tolls and bus lane distance, 2015-2017.
*==================================================================

use    "${dataout}MainDataset" , clear

keep if couple==1
keep if year>=2015 & year<=2017

label variable toll_fam_mean "Road toll (NOK)" 
label variable ptl_fam_km_mean "Bus lane (km)"


*============================================================================
// Absorb 3 way FE from variables 
*============================================================================


local xvarlist  toll_fam_mean ptl_fam_km_mean  

foreach x in `xvarlist' {
capt drop res_`x'
capt drop resM_`x'
reghdfe `x'  i.year , absorb($FE)  residuals(res_`x') 
sum `x'  if e(sample)==1
return list
dis "`r(mean)'"
gen resM_`x'=  res_`x' + `r(mean)'
}



*===================================================
//  Histogram - residualized (with mean)
*===================================================


local xvarlist   toll_fam_mean ptl_fam_km_mean  
foreach x in `xvarlist' {
local labx: variable label `x'

sum resM_`x' , det
histogram resM_`x'   if resM_`x'   >`r(p1)'  & resM_`x'  <`r(p99)' ///
, percent scheme(s2mono) graphregion(color(white))  xtitle("`labx'") scale(1.3) ///
color(gs10)  lcolor(gs5)
graph export    "${figures}FigureA6_`x'.png" ,  replace
graph export    "${figures}FigureA6_`x'.pdf" ,  replace

}

*------------
log close
